package com.xzzz.irda.guardcat.core.util;

import java.text.DecimalFormat;

/**
 * @author xzzz
 */
@SuppressWarnings("all")
public class GuardcatUtil {

    /**
     * 秒转换为 x天x小时x分x秒 的格式
     *
     * @param second 秒
     * @return 转换为 x天x小时x分x秒 的格式
     */
    public static String secondToDay(long second) {
        long days = second / 86400; //转换天数
        second = second % 86400;    //剩余秒数
        long hours = second / 3600; //转换小时
        second = second % 3600;     //剩余秒数
        long minutes = second / 60;  //转换分钟
        second = second % 60;       //剩余秒数
        if (days > 0) {
            return days + "天" + hours + "小时" + minutes + "分" + second + "秒";
        } else {
            return hours + "小时" + minutes + "分" + second + "秒";
        }
    }


    private static final int GB = 1024 * 1024 * 1024;//定义GB的计算常量
    private static final int MB = 1024 * 1024;//定义MB的计算常量
    private static final int KB = 1024;//定义KB的计算常量

    public static double byteToMB(long bytes) {
        //获取到的size为：1705230
        DecimalFormat df = new DecimalFormat("0.00");//格式化小数
        String resultSize = "";
        if (bytes / MB >= 1) {
            //如果当前Byte的值大于等于1MB
            resultSize = df.format(bytes / (float) MB);
        } else if (bytes / KB >= 1) {
            //如果当前Byte的值大于等于1KB
            resultSize = df.format(bytes / (float) KB);
        } else {
            resultSize = bytes + "";
        }
        return Double.parseDouble(resultSize);
    }

    public static String byteToGB(long bytes) {
        //获取到的size为：1705230
        DecimalFormat df = new DecimalFormat("0.00");//格式化小数
        String resultSize = "";
        if (bytes / GB >= 1) {
            //如果当前Byte的值大于等于1GB
            resultSize = df.format(bytes / (float) GB) + "GB   ";
        } else if (bytes / MB >= 1) {
            //如果当前Byte的值大于等于1MB
            resultSize = df.format(bytes / (float) MB) + "MB   ";
        } else if (bytes / KB >= 1) {
            //如果当前Byte的值大于等于1KB
            resultSize = df.format(bytes / (float) KB) + "KB   ";
        } else {
            resultSize = bytes + "B   ";
        }
        return resultSize;
    }


}
